package com.dhl.rabbitmq.five;

import com.dhl.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.*;
import sun.plugin2.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * 交换机模式,消费者，讲消息打印到控制台
 * @author danghailiang
 * @date 2022-12-07
 */
public class ReceiveLogs01 {
    public static final String EXCHANGE_NAME = "logs";
    public static void main(String[] args) throws Exception {
        // 1.获取信道
        Channel channel = RabbitUtils.getChannel();
        // 2.创建交换机,设置类型为广播模式
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
        // 3.创建临时队列
        String queueName = channel.queueDeclare().getQueue();
        // 4.交换机和队列绑定
        channel.queueBind(queueName,EXCHANGE_NAME,"");
        // 5.接受消息
        DeliverCallback deliverCallback = (String consumerTag, Delivery message) ->{
            System.out.println("FANOUT模式ReceiveLogs01接收到的消息为：" + consumerTag + "，"+ new String(message.getBody(),StandardCharsets.UTF_8));
        };
        channel.basicConsume(queueName,true,deliverCallback,(consumerTag,message) ->{});
    }

}
